﻿Public Class clsLXCK
    Public Function getList(prod_id As String, location As String,
                            time_start As String, time_end As String) As DataTable
        Dim szSql As String = ""
        Dim dt As DataTable = Nothing
        szSql &= "select * from sf.b_lx_ck where 1 = 1 "
        If String.IsNullOrEmpty(prod_id) = False Then
            szSql &= " and prod_id like '%" + prod_id + "%'"
        End If
        If String.IsNullOrEmpty(location) = False Then
            szSql &= " and location = '" + location + "'"
        End If
        If String.IsNullOrEmpty(time_start) = False And String.IsNullOrEmpty(time_end) = False Then
            szSql &= " and (into_date >= '" + DateTime.Parse(time_start + " 00:00:00.000") + "'"
            szSql &= " and into_date <= '" + DateTime.Parse(time_end + " 23:59:59.999") + "')"
            szSql &= " or (out_date >= '" + DateTime.Parse(time_start + " 00:00:00.000") + "'"
            szSql &= " and out_date <= '" + DateTime.Parse(time_end + " 23:59:59.999") + "')"
        End If
        Return DBacs.ExecuteSql(szSql, -1)
    End Function

    Public Function getListByProdId(prodid As String, status As String) As DataTable
        Dim szSql As String = ""
        Dim dt As DataTable = Nothing
        szSql &= "select a.*,b.type_value from sf.b_lx_ck a "
        szSql &= "left join sf.b_pgd_mes b on a.b_pgd_mes_sys_id = b.sys_id "
        szSql &= "where 1 = 1 "
        szSql &= " and a.prod_id = '" + prodid + "'"
        szSql &= " and a.status = '" + status + "'"
        Return DBacs.ExecuteSql(szSql, -1)
    End Function

    Public Function intoCK(en As entity_lx_ck) As Integer
        Dim sql As String = String.Empty
        Dim rtn As Integer = 0
        DBacs.BeginTransaction()
        Try
            sql &= "INSERT INTO sf.b_prod_lxjc_info(factory_id,prod_id,b_pgd_mes_sys_id,update_time)"
            sql &= "VALUES (? , ? , ? , (SELECT CURRENT_TIMESTAMP(6)))"
            rtn = DBacs.ExecuteUpdateSql(sql, -1, en.factory_id, en.prod_id, en.b_pgd_mes_sys_id)
            sql = String.Empty
            sql &= "update sf.b_prod_info set update_time=(SELECT CURRENT_TIMESTAMP(6)),"
            sql &= "b_pgd_lx_details_sys_id=?, status=? "
            sql &= "where factory_id = ? and prod_id = ?"
            rtn = DBacs.ExecuteUpdateSql(sql, -1, en.b_pgd_mes_sys_id, "11", g_factory_id, en.prod_id)
            sql = String.Empty
            sql &= "INSERT INTO sf.b_lx_ck( factory_id, area, location, prod_id, b_pgd_mes_sys_id, into_date, status, into_ope, update_time)  "
            sql &= "VALUES ( ?, ?, ?, ?, ?, (SELECT CURRENT_TIMESTAMP(6)), ?, ?, (SELECT CURRENT_TIMESTAMP(6)))"
            With en
                rtn = DBacs.ExecuteUpdateSql(sql, -1, .factory_id, .area, .location, .prod_id, .b_pgd_mes_sys_id, .status, .into_ope)
            End With
            sql = String.Empty
            sql &= "update sf.b_prod_info set update_time = (SELECT CURRENT_TIMESTAMP(6)), status = ? "
            sql &= " where factory_id = ? and prod_id = ?"
            rtn = DBacs.ExecuteUpdateSql(sql, -1, "12", "TS", en.prod_id)
            'sql = String.Empty
            'sql &= "update sf.b_pgd_mes set "
            'sql &= " finish_count = finish_count + 1 "
            'sql &= " where sys_id = ? "
            'rtn = DBacs.ExecuteUpdateSql(sql, -1, en.b_pgd_mes_sys_id)
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try
        Return rtn
    End Function

    Public Function update_IntoCK_MES(prod_id As String, status As String, mes_sync_result_into As String) As Integer
        Dim szSql As String = "UPDATE sf.b_lx_ck SET mes_sync_result_into = ? "
        szSql &= " WHERE prod_id = ? and status = ? "
        Return DBacs.ExecuteUpdateSql(szSql, -1, mes_sync_result_into, prod_id, status)
    End Function

    Public Function update_OutCK_MES(prod_id As String, status As String, mes_sync_result_out As String) As Integer
        Dim szSql As String = "UPDATE sf.b_lx_ck SET mes_sync_result_out = ? "
        szSql &= " WHERE prod_id = ? and status = ? "
        Return DBacs.ExecuteUpdateSql(szSql, -1, mes_sync_result_out, prod_id, status)
    End Function

    Public Function outCK(_loc As String, oper As String, _sys_id As Integer, prod_id As String) As Integer
        Dim sql As String = ""
        Dim rtn As Integer = 0
        DBacs.BeginTransaction()
        Try
            sql &= " UPDATE sf.b_lx_ck "
            sql &= " SET out_date = (SELECT CURRENT_TIMESTAMP(6)) , status = ?, out_ope = ? "
            sql &= " WHERE factory_id = ? and sys_id = ? "

            rtn = DBacs.ExecuteUpdateSql(sql, -1, "1", oper, "TS", _sys_id)
            sql = String.Empty
            sql &= "update sf.b_prod_info set update_time=(SELECT CURRENT_TIMESTAMP(6)),"
            sql &= "status= ? "
            sql &= " where factory_id = ? and prod_id = ?"
            rtn = DBacs.ExecuteUpdateSql(sql, -1, "13", "TS", prod_id)
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try
        Return rtn
    End Function

    Public Function outCKs(_sys_ids As String, prod_ids As String, oper As String) As Integer
        Dim sql As String = ""
        Dim rtn As Integer = 0
        DBacs.BeginTransaction()
        Try
            sql &= " UPDATE sf.b_lx_ck "
            sql &= " SET out_date = (SELECT CURRENT_TIMESTAMP(6)) , status = ?, out_ope = ? "
            sql &= " WHERE sys_id in ("
            sql &= _sys_ids
            sql &= ") "
            rtn = DBacs.ExecuteUpdateSql(sql, -1, "1", oper)
            sql = String.Empty
            sql &= "update sf.b_prod_info set update_time=(SELECT CURRENT_TIMESTAMP(6)),"
            sql &= "status = ? "
            sql &= " where prod_id in ("
            sql &= prod_ids
            sql &= ") "
            DBacs.ExecuteUpdateSql(sql, -1, "13")
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try
        Return rtn
    End Function

End Class
